#include <stdio.h>
#include "ofc/DConfig.h"
#include "ofc/DFile.h"

#ifdef HAVE_DCONFIG
@interface MyHandler : Object <DConfigHandler>  // MyHander implements the DConfigHander interface
{
}
- (BOOL) startConfig;
- (BOOL) endConfig;
- (BOOL) section :(const char *) name;
- (BOOL) option :(const char *) section :(const char *) name :(const char *) value;
- (BOOL) comment :(const char *) comment;
- (void) error :(int) number :(const char *) name :(int) lineNumber :(int) columnNumber;
@end

@implementation MyHandler

- (BOOL) startConfig
{
  return YES;                          // No interrest in startConfig..
}

- (BOOL) endConfig
{
  return YES;                          // No interest in endConfig..
}

- (BOOL) section :(const char *) name
{
  return YES;                          // No interest in (start of) section
}

- (BOOL) option :(const char *) section :(const char *) name :(const char *) value
{
  printf("Section:%s Name:%s Value:%s\n", section, name, value);
  return YES;
}

- (BOOL) comment :(const char *) comment
{
  return YES;                          // No interest in comment
}

- (void) error :(int) number :(const char *) name :(int) lineNumber :(int) columnNumber
{
  printf("Error:%d in %s on line %d and column %d.\n", number, name, lineNumber, columnNumber);
}

@end
#endif

int main(int argc, char *argv[])
{
#ifdef HAVE_DCONFIG 
  MyHandler     *hdlr = [MyHandler     new];  // Config handler
  DConfigReader *rdr  = [DConfigReader new];  // Config reader
  DFile         *file = [DFile         new];

  if ([file open :"example.ini" :"r"]) // Open the config file
  {
    if (![rdr parse :file :"example.ini" :hdlr]) // Parse the config file, calling the config handler methods
    {
      printf("File \"example.ini\" could not be parsed.\n");
    }

    [file close];
  }

  [hdlr free];                         // Cleanup
  [file free];
  [rdr  free];
#else
  printf("DConfig is not present in the library.\n");
#endif
  return 0;
}

